#ifndef PORTIO_H
#define PORTIO_H

class PortUser
{
	friend class PortIO;

	public:
		PortUser();
		virtual ~PortUser();

		virtual bool8 initOk();

		void getClientIP(char* buf, int32 maxBufSizeIncludingNull);

		LPOVERLAPPED getOver();

		HANDLE getHandle() { return handle; } 

	protected:
		virtual void ioDone(DWORD Amt) = 0;
		virtual void ioErr(DWORD) = 0;
		virtual void ioTimer(DWORD) {};

	protected:
		LPOVERLAPPED over;
		HANDLE handle;
};

inline LPOVERLAPPED 
PortUser::getOver()
{
	return over;
}

//port io keys
#define KEY_SHUTDOWN	0
#define KEY_USERIO		1
#define KEY_TIMER		2
#define KEY_CLOSED		3

class PortIO
{
	public:
		PortIO(int32 numWorkers);

		virtual ~PortIO();

		virtual bool8 initOk();

		void stop();

		bool8 newPortUser(PortUser*);

		HANDLE getPort();

	protected:
		bool8 start();
		static uint32 __stdcall workerProc(PortIO*);

	protected:
		HANDLE	port;
		int32	numWorkers;
		HANDLE* workers;
};

inline HANDLE
PortIO::getPort()
{
	return port;
}

#endif
